package cn.edu.ujn.lab3.service;

import cn.edu.ujn.lab3.dao.*;
import cn.edu.ujn.lab3.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 货品Service接口实现类
 */
@Service("vehicleService")
@Transactional
public class VehicleServiceImpl implements VehicleService {
	// 注入vehicleMapper
	@Autowired
	private VehicleMapper vehicleMapper;


    @Override
    public Page<Vehicle> findVehicleList(Integer page, Integer rows, String vehicleNo, String vehicleType) {
        Vehicle vehicle = new Vehicle();
        vehicle.setVehicleNo(vehicleNo);
        vehicle.setVehicleType(vehicleType);
        // 当前页 
        vehicle.setStart((page-1) * rows) ;
        // 每页数
        vehicle.setRows(rows);
        // 查询记录列表
        List<Vehicle> vehicles =
                vehicleMapper.selectVehicleList(vehicle);
        // 查询客户列表总记录数
        Integer count = vehicleMapper.selectVehicleListCount(vehicle);
        // 创建Page返回对象
        Page<Vehicle> result = new Page<>();
        result.setPage(page);
        result.setRows(vehicles);
        result.setSize(rows);
        result.setTotal(count);
        return result;
    }

    @Override
    public List<Vehicle> selectList(Vehicle vehicle) {
        return vehicleMapper.selectVehicleList(vehicle);
    }

    @Override
    public int createVehicle(Vehicle vehicle) {
        return vehicleMapper.insert(vehicle);
    }

    @Override
    public int updateVehicle(Vehicle vehicle) {
        return vehicleMapper.updateByPrimaryKey(vehicle);
    }

    @Override
    public int deleteVehicle(Integer id) {
        return vehicleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Vehicle getVehicleById(Integer id) {
        return vehicleMapper.selectByPrimaryKey(id);
    }
}
